package de.codi.similarity.sparematrix;

import org.semanticweb.owlapi.model.OWLEntity;

import de.codi.exception.CodiException;
import de.unima.alcomox.exceptions.CorrespondenceException;
import de.unima.alcomox.mapping.Correspondence;
import de.unima.alcomox.mapping.SemanticRelation;


public class Cell<E extends OWLEntity> implements Comparable<Cell<?>>
{
    private E      entity1;
    private E      entity2;
    private double confidenceValue;


    public Cell(E entity1, E entity2, double confidenceValue)
    {
        super();
        this.entity1 = entity1;
        this.entity2 = entity2;
        this.confidenceValue = confidenceValue;
    }


    public void setConfidenceValue(double confidenceValue)
    {
        this.confidenceValue = confidenceValue;
    }


    public double getConfidenceValue()
    {
        return confidenceValue;
    }


    public OWLEntity getSourceEntity()
    {
        return entity1;
    }


    public OWLEntity getTargetEntity()
    {
        return entity2;
    }


    public String[] toRockitString()
    {
        return new String[] { entity1.toString(), entity2.toString(), Double.toString(this.confidenceValue) };
    }


    public Correspondence toCorrespondence() throws CodiException
    {
        SemanticRelation equiv = null;
        try {
            equiv = new SemanticRelation(SemanticRelation.EQUIV);
            Correspondence c = new Correspondence(this.entity1.getIRI().toURI().toString(), this.entity2.getIRI().toURI().toString(), equiv, this.confidenceValue);
            return c;

        } catch(CorrespondenceException e) {
            throw new CodiException("failure in converting matrix cell to correspondence", e);

        }
    }


    @Override
    public int compareTo(Cell<?> o)
    {
        if(this.confidenceValue < o.confidenceValue) {
            return 1;
        } else if(this.confidenceValue > o.confidenceValue) {
            return -1;
        } else {
            if(!this.entity1.equals(o.entity1)) {
                return entity1.compareTo(o.entity1);
            } else {
                return entity2.compareTo(o.entity2);
            }
        }
    }
}